package com.sskj.fabi.ui.fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.github.tifezh.kchartlib.utils.DateUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.PublishBean;
import com.sskj.fabi.presenter.FabiPublishRecordFragmentPresenter;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseFragment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class FabiPublishRecordFragment extends BaseFragment<FabiPublishRecordFragmentPresenter> {
    @BindView(R2.id.publish_record_rcv)
    RecyclerView publishRecordRcv;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private boolean isBuy; // true 购买  false出售
    private BaseAdapter<PublishBean> mAdapter;
    private int PAGE = 1;
    private int PAGESIZE = 6;
    private AlertDialog tipDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.fabi_fragment_fabi_publish_record;
    }

    @Override
    protected FabiPublishRecordFragmentPresenter getPresenter() {
        return new FabiPublishRecordFragmentPresenter();
    }

    public static FabiPublishRecordFragment newInstance(boolean flag) {
        Bundle args = new Bundle();
        args.putBoolean("isBuy", flag);
        FabiPublishRecordFragment fragment = new FabiPublishRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initView() {
        super.initView();
        isBuy = getArguments().getBoolean("isBuy");
        publishRecordRcv.setLayoutManager(new LinearLayoutManager(getActivity()));
        publishRecordRcv.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setLastDraw(false)
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.fabi_backgroud))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 10)));
        mAdapter = new BaseAdapter<PublishBean>(R.layout.fabi_publish_record_item, null, publishRecordRcv) {
            @Override
            public void bind(ViewHolder holder, PublishBean item) {
                holder.setText(R.id.tvRecordDate, "创建时间:" + TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(item.getCreateTime()) * 1000))));
                holder.setText(R.id.tvStatus, "1".equals(item.getStatus()) ? "撤单" : "2".equals(item.getStatus()) ? "已完成" : "已撤单");
                holder.setText(R.id.publish_record_price, (!isBuy ? "出售单价: " : "购买单价: ") + "¥ " + item.getPrice());
                holder.setText(R.id.publish_record_num, (!isBuy ? "出售数量: " : "购买数量: ") + item.getTransNum() + "BDD");
                holder.setText(R.id.publish_record_limt, (!isBuy ? "出售总价: " : "购买总价: ") + "¥ " + item.getTotalPrice());
                holder.setText(R.id.publish_record_min_limt, "最小限额: " + "¥ " + item.getMinPrice());
                holder.setText(R.id.publish_record_max_limt, "最大限额: " + "¥ " + item.getMaxPrice());
                ((ImageView) holder.getView(R.id.iv_alipay)).setVisibility(1 == item.getPay_alipay() ? View.VISIBLE : View.GONE);
                ((ImageView) holder.getView(R.id.iv_wechat)).setVisibility(1 == item.getPay_wx() ? View.VISIBLE : View.GONE);
                ((ImageView) holder.getView(R.id.iv_bank)).setVisibility(1 == item.getPay_backcard() ? View.VISIBLE : View.GONE);
                ((ImageView) holder.getView(R.id.iv_palpay)).setVisibility(1 == item.getPaypal() ? View.VISIBLE : View.GONE);
                ClickUtil.click(holder.getView(R.id.ll_status), () -> {
                    if ("1".equals(item.getStatus())) {
                        showCancelTip(item);
                    }
                });
            }
        };
        mAdapter.setEmptyView();
        smartRefreshLayout.setOnRefreshListener(refreshLayout1 -> {  // 下拉刷新
            smartRefreshLayout.finishRefresh();
            PAGE = 1;
            mPresenter.getFaBuRecord(isBuy ? "pmma" : "sell", PAGE, PAGESIZE);
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {  // 上拉加载
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                smartRefreshLayout.finishLoadMore();
                PAGE++;
                mPresenter.getFaBuRecord(isBuy ? "pmma" : "sell", PAGE, PAGESIZE);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getFaBuRecord(isBuy ? "pmma" : "sell", PAGE, PAGESIZE);
    }

    public void setData(List<PublishBean> listData) {
        if (PAGE == 1) {
            mAdapter.setNewData(listData);
        } else {
            mAdapter.addData(listData);
        }
    }

    /**
     * 显示撤单提示
     */
    private void showCancelTip(PublishBean item) {
        if (tipDialog == null) {
            tipDialog = new AlertDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setMessage("是否撤销订单")
                    .setPositiveButton("确定", (dialog, which) -> {
                        dialog.dismiss();
                        mPresenter.cancleOrder(isBuy ? "1" : "2", item.getId());
                    })
                    .setNegativeButton(App.INSTANCE.getString(R.string.fabi_dialog_appeal84), (dialog, which) -> dialog.dismiss()).create();
        }
        tipDialog.show();
        Button btnPos = tipDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        Button btnNeg = tipDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        btnPos.setTextColor(Color.BLACK);
        btnNeg.setTextColor(Color.BLACK);
    }

    public void updateUi() {
        PAGE = 1;
        mPresenter.getFaBuRecord(isBuy ? "pmma" : "sell", PAGE, PAGESIZE);
    }
}
